/**
 * message
 * 使用方法：this.$herinmessage.success('str')
 */
import Vue from 'vue';
import Main from './HerinMessage.vue';
const MessageConstructor = Vue.extend(Main);
let instance;
const instances = [];
let seed = 1;

const Message = function(options) {
  if (Vue.prototype.$isServer) return;
  options = options || {};
  if (typeof options === 'string') {
    options = {
      message: options
    };
  }
  const id = 'message_' + seed++;

  options.onClose = function() {
    Message.close(id);
  };

  instance = new MessageConstructor({
    data: options
  });

  instance.id = id;
  instance.$mount();
  document.body.appendChild(instance.$el);
  // instance.verticalOffset += 62 * instances.length
  instance.visible = true;
  instances.push(instance);

  return instance;
};

['success', 'warn', 'error'].forEach(type => {
  Message[type] = options => {
    if (typeof options === 'string') {
      options = {
        message: options
      };
    }
    options.type = type;
    return Message(options);
  };
});

export default Message;
